#include "video_capturer.hpp"
#include "p2pms_log.hpp"

#include <stdint.h>

#include <memory>

#include "modules/video_capture/video_capture_factory.h"
#include "rtc_base/checks.h"
#include "rtc_base/logging.h"

namespace p2pms 
{

//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
VideoCapturer::VideoCapturer() : m_vcm(nullptr) {}

//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
bool VideoCapturer::Init(size_t width, size_t height, size_t fps, const std::string& device_id) 
{
	m_width = width;
	m_height = height;

	std::unique_ptr<webrtc::VideoCaptureModule::DeviceInfo> device_info(
		webrtc::VideoCaptureFactory::CreateDeviceInfo());

	int result = device_info->GetCapability(device_id.c_str(), 0, m_capability);
	if (result != 0) {
		LOG_ERR("Failed to get capability for device: {}", device_id);
		return false;
	}

	m_vcm = webrtc::VideoCaptureFactory::Create(device_id.c_str());
	if (!m_vcm) {
		LOG_ERR("Failed to create VideoCapturer for device: {}", device_id);
		return false;
	}
	m_vcm->RegisterCaptureDataCallback(this);

	m_capability.width = static_cast<int32_t>(width);
	m_capability.height = static_cast<int32_t>(height);
	m_capability.maxFPS = static_cast<int32_t>(fps);
	m_capability.videoType = webrtc::VideoType::kI420;

	if (m_vcm->StartCapture(m_capability) != 0) {
		LOG_ERR("Failed to start video capture for device: {}", device_id);
		Destroy();
		return false;
	}

	return true;
}

//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
VideoCapturer* VideoCapturer::Create(size_t width, size_t height, size_t fps,
  const std::string& device_id) 
{
	LOG_INF("Creating VideoCapturer with device_id: {}, width: {}, height: {}, fps: {}",
		device_id, width, height, fps);

	std::unique_ptr<VideoCapturer> vcm_capturer(new VideoCapturer());
	if (!vcm_capturer->Init(width, height, fps, device_id)) {
		LOG_ERR("Failed to initialize VideoCapturer");
		return nullptr;
	}

	return vcm_capturer.release();
}

//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void VideoCapturer::Destroy() 
{
	LOG_INF("Destroying VideoCapturer");

	if (m_vcm) {
		if (m_vcm->CaptureStarted()) {
			m_vcm->StopCapture();
		}
		m_vcm->DeRegisterCaptureDataCallback();
		m_vcm = nullptr; // Release reference to VCM.
	}
}

//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
VideoCapturer::~VideoCapturer() 
{
	Destroy();
}

//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void VideoCapturer::OnFrame(const webrtc::VideoFrame& frame) 
{
	CapturerVideoSource::OnFrame(frame);
}

}  // namespace p2pms
